import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;


public class Client 
{
	public static void main(String[] pArgs) 
	{
		if(pArgs.length<3)
		{
			System.out.println("usage: java Client host port boardnum");
			return;
		}
	
		try
		{
			Socket lSocket=new Socket(pArgs[0],Integer.parseInt(pArgs[1]));
			PrintWriter lOut=new PrintWriter(lSocket.getOutputStream());
			BufferedReader lIn=new BufferedReader(new InputStreamReader(lSocket.getInputStream()));
	
            lOut.println(pArgs[2]);
            lOut.flush();

            String lLine=lIn.readLine();

            //read number of rows
            int lNumRows=Integer.parseInt(lLine);
            
            Board.CellType[][] board = null;

            boolean first = true;
            
            //read each row
            for(int i=0;i<lNumRows;i++)
            {
                lLine=lIn.readLine();
                //here, we would store the row somewhere, to build our board
                //in this demo, we just print it
//                System.out.println(lLine);
                
                if ( first )
                {
                	board = new Board.CellType[ lLine.length() ][ lNumRows ];
                	first = false;
                }
                
                for ( int n = 0; n < lLine.length(); n++ )
                {
                	 board[ n ][ i ] =  Board.CellType.create( lLine.codePointAt(n) );    
                }
     
            }
                
            UsedSolutionFinder uSF = new UsedSolutionFinder();
            
//            Board ourBoard = new BoardImpl( board );
            Board ourBoard = new BoardImplFast( board );
            
            
            
//            String lMySol = findSolution( board );
//            String lMySol = solution.FindSolution(ourBoard);
            String lMySol = uSF.useSolutionFinder(ourBoard);
            
            System.out.println("Solution = "+ lMySol );

            //send the solution to the server
            lOut.println(lMySol);
            lOut.flush();
    
            //read answer from the server
            lLine=lIn.readLine();
    
            System.out.println(lLine);
		}
		catch(Throwable t)
		{
			t.printStackTrace();
		}
		
		// Test
//		System.exit(0);
		
	}

}
